package com.iecas.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.iecas.entity.User;
import com.iecas.utils.OnlineManager;
import com.iecas.utils.TokenUtils;

import java.util.ArrayList;
import java.util.List;

public class LoginService {
    public static List<User> users;

    static {
        users = new ArrayList<>();
        User user1 = new User();
        user1.setUuid("1");
        user1.setUsername("root");
        user1.setPassword("root");
        User user2 = new User();
        user2.setUuid("2");
        user2.setUsername("admin");
        user2.setPassword("admin");
        users.add(user1);
        users.add(user2);
    }

    public String login(String username, String password) {
        User find = users.stream().filter(user -> user.getUsername().equals(username)).filter(user -> user.getPassword().equals(password)).findFirst().orElse(null);
        if (ObjectUtil.isNotNull(find)) {
            String token = TokenUtils.gen();
            OnlineManager.set(token, find);
            return token;
        } else {
            return null;
        }
    }

    public void logout(String token) {
        OnlineManager.remove(token);
    }
}
